package org.example.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.example.annotation.KafkaProcesser;
import org.example.dao.CustomInfoMapper;
import org.example.model.po.CustomInfo;
import org.example.service.CustomInfoService;
import org.example.service.KafkaProcessHandler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 聚合支付平台-C端客户信息表(TCustomInfo)表服务实现类
 *
 * @author makejava
 * @since 2020-05-06 10:15:04
 */
@Service("customInfoService")
@KafkaProcesser(key = "customInfo")
@Slf4j
public class CustomInfoServiceImpl implements CustomInfoService, KafkaProcessHandler {
    @Resource
    private CustomInfoMapper customInfoMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public CustomInfo queryById(Long id) {
        return this.customInfoMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    public List<CustomInfo> queryAllByLimit(int offset, int limit) {
        return this.customInfoMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param customInfo 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public CustomInfo insert(CustomInfo customInfo) {
        this.customInfoMapper.insert(customInfo);
        return customInfo;
    }

    /**
     * 修改数据
     *
     * @param customInfo 实例对象
     * @return 实例对象
     */
    @Transactional(rollbackFor = Exception.class)
    public CustomInfo update(CustomInfo customInfo) {
        this.customInfoMapper.update(customInfo);
        return this.queryById(customInfo.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        return this.customInfoMapper.deleteById(id) > 0;
    }

    /**
     * @param message
     */
    public void handler(String message) {
        CustomInfo customInfo = JSON.parseObject(message, CustomInfo.class);
        customInfo.setInstallmentNumber("qw34");
        customInfo.setCrtTime("2020-05-06 15:45");
        customInfo.setUpdTime("2020-05-06 15:45");
        customInfo = this.insert(customInfo);
        log.info("持久化后[customInfo]数据 \n {}", JSON.toJSONString(customInfo));
    }
}